package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;

import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Memberf;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.service.PhysicalService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.text.SimpleDateFormat;
import java.util.*;




@RestController
@RequestMapping("/member")
public class MemberController {

    @Reference
    private MemberService memberService;

    @Reference
    private OrderService orderService;
    @Reference
    private PhysicalService physicalService;

    @RequestMapping("/getMemberCount")
    public Result getMemberCount(String startTime, String endTime) {
        try {
            List<String> result = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
            Calendar min = Calendar.getInstance();//获取最小时间
            Calendar max = Calendar.getInstance();//获取最大时间
            min.setTime(sdf.parse(startTime));  //将startTime格式化并定义为Calendar中的min对象
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);//运用Calendar中方法，将日期定义为某年某月1日
            max.setTime(sdf.parse(endTime));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
            Calendar curr = min;
            while (curr.before(max)) {//将max min 日期进行比较 小则将月份加入到集合中 并在月份加一
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.MONTH, 1);
            }
            List<Integer> memberCountList = memberService.findMemberCountByMouth(result);

            Map<String, Object> map = new HashMap<>();
            map.put("months", result);
            map.put("memberCount", memberCountList);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);

        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @RequestMapping("/getOrderCount")
    public Result getOrderCount(String startTime, String endTime) {
        try {
            List<String> result = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
            Calendar min = Calendar.getInstance();//获取最小时间
            Calendar max = Calendar.getInstance();//获取最大时间
            min.setTime(sdf.parse(startTime));  //将startTime格式化并定义为Calendar中的min对象
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);//运用Calendar中方法，将日期定义为某年某月1日
            max.setTime(sdf.parse(endTime));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
            Calendar curr = min;
            while (curr.before(max)) {//将max min 日期进行比较 小则将月份加入到集合中 并在月份加一
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.MONTH, 1);
            }
            List<Integer> orderCountList = orderService.findOrderCountByMouth(result);

            Map<String, Object> map = new HashMap<>();
            map.put("months", result);
            map.put("orderCount", orderCountList);
            return new Result(true, MessageConstant.GET_ORDER_NUMBER_REPORT_SUCCESS, map);
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_ORDER_NUMBER_REPORT_FAIL);
        }
    }

    @RequestMapping("/getPhysicalCount")
    public Result getPhysicalCount(String startTime, String endTime) {
        try {
            List<String> result = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
            Calendar min = Calendar.getInstance();//获取最小时间
            Calendar max = Calendar.getInstance();//获取最大时间
            min.setTime(sdf.parse(startTime));  //将startTime格式化并定义为Calendar中的min对象
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);//运用Calendar中方法，将日期定义为某年某月1日
            max.setTime(sdf.parse(endTime));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
            Calendar curr = min;
            while (curr.before(max)) {//将max min 日期进行比较 小则将月份加入到集合中 并在月份加一
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.MONTH, 1);
            }
            List<Integer> physicalCountList = physicalService.findPhysicalCountByMouth(result);

            Map<String, Object> map = new HashMap<>();
            map.put("months", result);
            map.put("physicalCount", physicalCountList);
            return new Result(true, MessageConstant.GET_PHYSICAL_NUMBER_REPORT_SUCCESS, map);
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_PHYSICAL_NUMBER_REPORT_FAIL);
        }
    }
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult page = memberService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return page;
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Memberf memberf){
        try {
            memberService.addAll(memberf);
        }catch (Exception e){
//            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
            return new Result(false,"添加失败");
        }
//        return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
        return new Result(true,"添加成功");
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            memberService.deleteById(id);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Memberf memberf = memberService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,memberf);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Memberf memberf){
        try{
            memberService.edit(memberf);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }
}

